<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<!-- 查询表单区域 -->
			<el-row v-show="showSearch">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
					<el-form-item label="设备sn" prop="deviceSn">
						<el-input placeholder="请输入设备sn" v-model="state.queryForm.deviceSn" />
					</el-form-item>
					<el-form-item label="下发时间" prop="sendSuccessTime">
						<el-date-picker
							type="datetimerange"
							start-placeholder="请选择开始时间"
							end-placeholder="请选择结束时间"
							v-model="state.queryForm.sendSuccessTime"
							value-format="YYYY-MM-DD HH:mm:ss"
						/>
					</el-form-item>
					<el-form-item label="下发内容" prop="sendMsg">
						<el-input placeholder="请输入下发内容" v-model="state.queryForm.sendMsg" />
					</el-form-item>
          <el-form-item label="下发状态" prop="status">
            <el-select placeholder="请选择下发状态" v-model="state.queryForm.status" clearable>
              <el-option label="未下发" value="0" />
              <el-option label="已下发" value="1" />
              <el-option label="下发成功" value="2" />
            </el-select>
          </el-form-item>
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList"> 查询 </el-button>
						<el-button icon="Refresh" @click="resetQuery">重置</el-button>
					</el-form-item>
				</el-form>
			</el-row>

			<!-- 操作按钮区域 -->
			<el-row>
				<div class="mb8" style="width: 100%">
					<right-toolbar
						v-model:showSearch="showSearch"
						:export="'admin_deviceAlarm_export'"
						@exportExcel="exportExcel"
						class="ml10 mr20"
						style="float: right"
						@queryTable="getDataList"
					/>
				</div>
			</el-row>

			<!-- 数据表格区域 -->
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column type="index" label="#" width="50" />
				<el-table-column prop="deviceSn" label="设备sn" show-overflow-tooltip />
				<el-table-column prop="deviceName" label="设备名称" show-overflow-tooltip />
				<el-table-column prop="sendSuccessTime" label="下发时间" show-overflow-tooltip />
				<el-table-column prop="sendMsg" label="下发内容" show-overflow-tooltip />
				<el-table-column prop="status" label="状态" show-overflow-tooltip >
          <template #default="{ row }">
            <el-tag v-if="row.status == 0" type="danger">未下发</el-tag>
            <el-tag v-else-if="row.status == 1" type="success">已下发</el-tag>
            <el-tag v-else-if="row.status == 2" type="success">下发成功</el-tag>
            <el-tag v-else>未知</el-tag>
          </template>
        </el-table-column>
			</el-table>

			<!-- 分页组件 -->
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 编辑、新增弹窗 -->
		<form-dialog ref="formDialogRef" @refresh="getDataList(false)" />
	</div>
</template>

<script setup lang="ts" name="systemDeviceCmd">
// ========== 导入声明 ==========
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/deviceCmd/deviceCmd';
import { useMessage, useMessageBox } from '/@/hooks/message';

// ========== 组件声明 ==========
// 异步加载表单弹窗组件
const FormDialog = defineAsyncComponent(() => import('./form.vue'));

// ========== 字典数据 ==========

// ========== 组件引用 ==========
const formDialogRef = ref(); // 表单弹窗引用
const queryRef = ref(); // 查询表单引用

// ========== 响应式数据 ==========
const showSearch = ref(true); // 是否显示搜索区域
const selectObjs = ref([]) as any; // 表格多选数据
const multiple = ref(true); // 是否多选

// ========== 表格状态 ==========
const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {}, // 查询参数
	pageList: (query) => {
		return fetchList({
			...query,
      sendSuccessTime: undefined,
      sendSuccessTimeStart: query.sendSuccessTime?.[0],
			sendSuccessTimeEnd: query.sendSuccessTime?.[1],
		});
	}, // 分页查询方法
});

// ========== Hook引用 ==========
// 表格相关Hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, downBlobFile, tableStyle } = useTable(state);

// ========== 方法定义 ==========
/**
 * 重置查询条件
 */
const resetQuery = () => {
	// 清空搜索条件
	queryRef.value?.resetFields();
	// 清空多选
	selectObjs.value = [];
	// 重新查询
	getDataList();
};

/**
 * 导出Excel文件
 */
const exportExcel = () => {
	downBlobFile('/admin/deviceAlarm/export', Object.assign(state.queryForm, { ids: selectObjs }), 'deviceAlarm.xlsx');
};

/**
 * 表格多选事件处理
 * @param objs 选中的数据行
 */
const selectionChangHandle = (objs: { alarmId: string }[]) => {
	selectObjs.value = objs.map(({ alarmId }) => alarmId);
	multiple.value = !objs.length;
};

/**
 * 删除数据处理
 * @param ids 要删除的数据ID数组
 */
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
